from rest_framework import serializers

from apps.goods.models import Goods
from apps.order.models import Order, OrderDetail
from jingxi_shop_project.settings import IMAGE_URL


class OrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields = '__all__'

class OrderDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderDetail
        fields = '__all__'

class OrederManySerializer(serializers.ModelSerializer):
    order_info = serializers.SerializerMethodField()

    def get_order_info(self, obj):
        info = OrderDetail.objects.filter(trade_no=obj.trade_no).all()
        ser = OrderDetailSerializer(info, many=True).data

        for i in ser:
            goods_data = Goods.objects.filter(good_id=i.get('goods')).first()
            i['price'] = goods_data.price
            i['photo_URL'] = IMAGE_URL + goods_data.photo_URL
            i['name'] = goods_data.name
        return ser

    class Meta:
        model = Order
        fields = '__all__'